package app.imttf.online.model;

import app.imttf.online.adfextensions.CustomApplicationModuleImpl;
import app.imttf.online.model.common.OnlineTtfAM;
import app.imttf.online.model.view.admin.access.UserCabangViewImpl;
import app.imttf.online.model.view.admin.access.UserOlSupplierViewImpl;
import app.imttf.online.model.view.buatttf.HariLiburIndomarcoViewImpl;
import app.imttf.online.model.view.buatttf.InfoSupplierAttrbViewImpl;
import app.imttf.online.model.view.buatttf.NpwpIndomarcoViewImpl;
import app.imttf.online.model.view.buatttf.NumberTtfHeaderViewImpl;
import app.imttf.online.model.view.buatttf.NumberTtfHeaderViewRowImpl;
import app.imttf.online.model.view.buatttf.SupplierGiroTransferFlagViewImpl;
import app.imttf.online.model.view.buatttf.SupplierGiroTransferFlagViewRowImpl;
import app.imttf.online.model.view.buatttf.TanggalTtfHeaderViewImpl;
import app.imttf.online.model.view.buatttf.TanggalTtfHeaderViewRowImpl;
import app.imttf.online.model.view.buatttf.TtfBpbTotalAmountViewImpl;
import app.imttf.online.model.view.buatttf.TtfBpbTotalAmountViewRowImpl;
import app.imttf.online.model.view.buatttf.TtfFpTotalAmountViewImpl;
import app.imttf.online.model.view.buatttf.TtfFpTotalAmountViewRowImpl;
import app.imttf.online.model.view.buatttf.TtfHeaderGetKeyViewImpl;
import app.imttf.online.model.view.buatttf.TtfHeaderMemoMdViewImpl;
import app.imttf.online.model.view.buatttf.TtfHeaderTotalAmountViewImpl;
import app.imttf.online.model.view.buatttf.TtfHeaderTotalAmountViewRowImpl;
import app.imttf.online.model.view.buatttf.TtfHeaderUpdateStatusViewImpl;
import app.imttf.online.model.view.buatttf.ValidTglJatuhTempoViewImpl;
import app.imttf.online.model.view.buatttf.ValidTglJatuhTempoViewRowImpl;
import app.imttf.online.model.view.buatttf.ValidTglWednesdayViewImpl;
import app.imttf.online.model.view.buatttf.ValidTglWednesdayViewRowImpl;
import app.imttf.online.model.view.buatttf.input.TtfFpInputViewImpl;
import app.imttf.online.model.view.buatttf.upload.ValidateSupCodeBranchViewImpl;
import app.imttf.online.model.view.deletettf.DeleteTtfBpbWhenCancelViewImpl;
import app.imttf.online.model.view.deletettf.DeleteTtfFpWhenCancelViewImpl;
import app.imttf.online.model.view.deletettf.DeleteTtfHeaderWhenCancelViewImpl;
import app.imttf.online.model.view.info.FlagPkpViewImpl;
import app.imttf.online.model.view.buatttf.upload.TtfBpbViewImpl;
import app.imttf.online.model.view.buatttf.upload.TtfFpViewImpl;
import app.imttf.online.model.view.info.InfoCabangViewImpl;
import app.imttf.online.model.view.info.InfoSupplierViewImpl;
import app.imttf.online.model.view.lov.LovSupplierImpl;
import app.imttf.online.model.view.param.ParamGlobalViewImpl;
import app.imttf.online.model.view.ttf.TtfHeaderViewImpl;
import app.imttf.online.model.view.validasi.CheckValLevel1CompleteViewImpl;
import app.imttf.online.model.view.validasi.CheckValLevel2CompleteViewImpl;
import app.imttf.online.model.view.validasi.NilaiToleransiDppViewImpl;
import app.imttf.online.model.view.validasi.NilaiToleransiDppViewRowImpl;
import app.imttf.online.model.view.validasi.NilaiToleransiPpnViewImpl;
import app.imttf.online.model.view.validasi.NilaiToleransiPpnViewRowImpl;
import app.imttf.online.model.view.validasi.TtfBpbUpdateValidateLvl2ViewImpl;
import app.imttf.online.model.view.validasi.TtfBpbUpdateValidateLvl2ViewRowImpl;
import app.imttf.online.model.view.validasi.TtfBpbUpdateValidateViewImpl;
import app.imttf.online.model.view.validasi.TtfBpbUpdateValidateViewRowImpl;
import app.imttf.online.model.view.validasi.TtfBpbValidationViewImpl;
import app.imttf.online.model.view.validasi.TtfBpbValidationViewRowImpl;
import app.imttf.online.model.view.validasi.TtfFpUpdateValidateLvl2ViewImpl;
import app.imttf.online.model.view.validasi.TtfFpUpdateValidateLvl2ViewRowImpl;
import app.imttf.online.model.view.validasi.TtfFpUpdateValidateViewImpl;
import app.imttf.online.model.view.validasi.TtfFpUpdateValidateViewRowImpl;
import app.imttf.online.model.view.validasi.TtfFpValidationViewImpl;
import app.imttf.online.model.view.validasi.TtfFpValidationViewRowImpl;
import app.imttf.online.model.view.validasi.ValidasiNilaiDppViewImpl;
import app.imttf.online.model.view.validasi.ValidasiNilaiDppViewRowImpl;
import app.imttf.online.model.view.validasi.ValidasiNilaiTaxViewImpl;
import app.imttf.online.model.view.validasi.ValidasiNilaiTaxViewRowImpl;
import app.imttf.online.model.view.validasi.ValidasiNoBpbViewImpl;
import app.imttf.online.model.view.validasi.ValidasiNpwpIndomarcoViewImpl;
import app.imttf.online.model.view.validasi.ValidasiNpwpSupplierViewImpl;
import app.imttf.online.model.view.validasi.ValidasiTglBpbViewImpl;
import app.imttf.online.model.view.validasi.ValidasiTglBpbViewRowImpl;
import app.imttf.online.model.view.validasi.ValidasiTglFpViewImpl;


import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.math.BigDecimal;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.sql.DataSource;

import oracle.jbo.domain.BlobDomain;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;

import oracle.jbo.server.ViewObjectImpl;

import org.krysalis.barcode4j.BarcodeDimension;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

import org.krysalis.barcode4j.output.bitmap.BitmapEncoder;
import org.krysalis.barcode4j.output.bitmap.BitmapEncoderRegistry;
import org.krysalis.barcode4j.tools.MimeTypes;
import org.krysalis.barcode4j.tools.UnitConv;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Tue Feb 18 11:24:11 ICT 2014
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class OnlineTtfAMImpl extends CustomApplicationModuleImpl implements OnlineTtfAM {
    /**
     * This is the default constructor (do not remove).
     */

    private StringBuilder yearNowStr;
    private SimpleDateFormat dateYearFormat = new SimpleDateFormat("yy");
    private final String ONLINE_FLAG = "1";

    public OnlineTtfAMImpl() {
    }

    public void setLoginToSession_OnlineTtfAM(String key, String val) {
        this.setLoginToSession(key, val);
    }

    /**
     * Container's getter for UserOlSupplierView1.
     * @return UserOlSupplierView1
     */
    public UserOlSupplierViewImpl getUserOlSupplierView1() {
        return (UserOlSupplierViewImpl)findViewObject("UserOlSupplierView1");
    }

    /**
     * Container's getter for InfoCabangView1.
     * @return InfoCabangView1
     */
    public ViewObjectImpl getInfoCabangView1() {
        return (ViewObjectImpl)findViewObject("InfoCabangView1");
    }


    /**
     * Container's getter for InfoSupplierView1.
     * @return InfoSupplierView1
     */
    public ViewObjectImpl getInfoSupplierView1() {
        return (ViewObjectImpl)findViewObject("InfoSupplierView1");
    }


    /**
     * Container's getter for FlagPkpView1.
     * @return FlagPkpView1
     */
    public FlagPkpViewImpl getFlagPkpView1() {
        return (FlagPkpViewImpl)findViewObject("FlagPkpView1");
    }

    /**
     * Container's getter for InfoSupplierAttrbView1.
     * @return InfoSupplierAttrbView1
     */
    public InfoSupplierAttrbViewImpl getInfoSupplierAttrbView1() {
        return (InfoSupplierAttrbViewImpl)findViewObject("InfoSupplierAttrbView1");
    }

    /**
     * Container's getter for VerifikasiNewTtfPkpView1.
     * @return VerifikasiNewTtfPkpView1
     */
    public ViewObjectImpl getVerifikasiNewTtfPkpView1() {
        return (ViewObjectImpl)findViewObject("VerifikasiNewTtfPkpView1");
    }

    /**
     * Container's getter for TtfBpbInputView1.
     * @return TtfBpbInputView1
     */
    public ViewObjectImpl getTtfBpbInputView1() {
        return (ViewObjectImpl)findViewObject("TtfBpbInputView1");
    }

    /**
     * Container's getter for TtfFpInputView1.
     * @return TtfFpInputView1
     */
    public ViewObjectImpl getTtfFpInputView1() {
        return (ViewObjectImpl)findViewObject("TtfFpInputView1");
    }

    /**
     * Container's getter for TtfBpbView1.
     * @return TtfBpbView1
     */
    public TtfBpbViewImpl getTtfBpbView1() {
        return (TtfBpbViewImpl)findViewObject("TtfBpbView1");
    }

    /**
     * Container's getter for TtfFpView1.
     * @return TtfFpView1
     */
    public TtfFpViewImpl getTtfFpView1() {
        return (TtfFpViewImpl)findViewObject("TtfFpView1");
    }

    /**
     * Container's getter for TtfHeaderView1.
     * @return TtfHeaderView1
     */
    public TtfHeaderViewImpl getTtfHeaderView1() {
        return (TtfHeaderViewImpl)findViewObject("TtfHeaderView1");
    }


    /**
     * Container's getter for VerifikasiNewTtfNonPkpView1.
     * @return VerifikasiNewTtfNonPkpView1
     */
    public ViewObjectImpl getVerifikasiNewTtfNonPkpView1() {
        return (ViewObjectImpl)findViewObject("VerifikasiNewTtfNonPkpView1");
    }

    /**
     * Container's getter for NpwpIndomarcoView1.
     * @return NpwpIndomarcoView1
     */
    public NpwpIndomarcoViewImpl getNpwpIndomarcoView1() {
        return (NpwpIndomarcoViewImpl)findViewObject("NpwpIndomarcoView1");
    }

    // Main Validation

    public void startValidationLvl1(String keyTtf, String siteCode,
                                    String branchCode, String noPkp) {

        // Faktur Pajak Validation
        TtfFpValidationViewImpl ttfFpValidationView =
            getTtfFpValidationView1();
        ttfFpValidationView.setkeyTtf(keyTtf);
        ttfFpValidationView.executeQuery();

        long ttfFpNumRow = ttfFpValidationView.getEstimatedRowCount();

        if (ttfFpNumRow > 0) {
            for (int y = 0; y < ttfFpNumRow; y++) {

                TtfFpValidationViewRowImpl ttfFpValidationRow =
                    (TtfFpValidationViewRowImpl)ttfFpValidationView.getRowSetIterator().next();

                String pkpSupplier = ttfFpValidationRow.getNoPkpSup();
                String pkpIndomarco = ttfFpValidationRow.getNoPkpIdm();
                String noFp = ttfFpValidationRow.getNoFp();
                String keyFpBpb = ttfFpValidationRow.getKeyFpBpb();

                //Validate Faktur Pajak Component Level 1
                validasiPkpSup(keyFpBpb, branchCode, siteCode, pkpSupplier);
                validasiPkpIndomarco(keyFpBpb, branchCode, pkpIndomarco);
                validasiFormatFp(keyFpBpb, noFp);

            }
        }

        // BPB Validation
        TtfBpbValidationViewImpl ttfBpbValidationView =
            getTtfBpbValidationView1();
        ttfBpbValidationView.setkeyTtf(keyTtf);
        ttfBpbValidationView.executeQuery();

        long ttfBpbNumRow = ttfBpbValidationView.getEstimatedRowCount();

        if (ttfBpbNumRow > 0) {
            for (int x = 0; x < ttfBpbNumRow; x++) {

                TtfBpbValidationViewRowImpl ttfBpbValidationRow =
                    (TtfBpbValidationViewRowImpl)ttfBpbValidationView.getRowSetIterator().next();

                String noBpb = ttfBpbValidationRow.getInvoiceNum();
                String keyFpBpb = ttfBpbValidationRow.getKeyFpBpb();

                // Validate BPB Component Level 1
                validasiNoBpb(keyFpBpb, noBpb, siteCode);
            }
        }

        this.getDBTransaction().commit();
    }

    public void startValidationLvl12(String keyTtf, String siteCode,
                                     String branchCode, String noPkp) {

        // Faktur Pajak Validation
        TtfFpValidationViewImpl ttfFpValidationView =
            getTtfFpValidationView1();
        ttfFpValidationView.setkeyTtf(keyTtf);
        ttfFpValidationView.executeQuery();

        long ttfFpNumRow = ttfFpValidationView.getEstimatedRowCount();

        if (ttfFpNumRow > 0) {
            for (int y = 0; y < ttfFpNumRow; y++) {

                TtfFpValidationViewRowImpl ttfFpValidationRow =
                    (TtfFpValidationViewRowImpl)ttfFpValidationView.getRowSetIterator().next();

                String pkpSupplier = ttfFpValidationRow.getNoPkpSup();
                String pkpIndomarco = ttfFpValidationRow.getNoPkpIdm();
                String noFp = ttfFpValidationRow.getNoFp();
                String keyFpBpb = ttfFpValidationRow.getKeyFpBpb();
                oracle.jbo.domain.Number amtTax =
                    ttfFpValidationRow.getAmountTax();

                // Validate Faktur Pajak Component Level 1
                validasiPkpSup(keyFpBpb, branchCode, siteCode, pkpSupplier);
                validasiPkpIndomarco(keyFpBpb, branchCode, pkpIndomarco);
                validasiFormatFp(keyFpBpb, noFp);

                // Validate Faktur Pajak Component Level 2
                validasiTglFp(keyFpBpb, amtTax);
                validasiNilaiDpp(keyFpBpb);
                validasiNilaiPpn(keyFpBpb);

            }
        }

        // BPB Validation
        TtfBpbValidationViewImpl ttfBpbValidationView =
            getTtfBpbValidationView1();
        ttfBpbValidationView.setkeyTtf(keyTtf);
        ttfBpbValidationView.executeQuery();

        long ttfBpbNumRow = ttfBpbValidationView.getEstimatedRowCount();

        if (ttfBpbNumRow > 0) {
            for (int x = 0; x < ttfBpbNumRow; x++) {

                TtfBpbValidationViewRowImpl ttfBpbValidationRow =
                    (TtfBpbValidationViewRowImpl)ttfBpbValidationView.getRowSetIterator().next();

                String noBpb = ttfBpbValidationRow.getInvoiceNum();
                String keyFpBpb = ttfBpbValidationRow.getKeyFpBpb();

                // Validate BPB Component Level 1
                validasiNoBpb(keyFpBpb, noBpb, siteCode);

                // Validate BPB Component Level 2
                validasiTglBpb(keyTtf, noBpb);

            }
        }

        this.getDBTransaction().commit();
    }

    // Validation Functions

    private void validasiPkpSup(String keyFpBpb, String branchCode,
                                String siteCode, String noPkp) {
        ValidasiNpwpSupplierViewImpl validasiNpwpSupplierView;
        validasiNpwpSupplierView = getValidasiNpwpSupplierView1();
        validasiNpwpSupplierView.setbranchCode(branchCode);
        validasiNpwpSupplierView.setnoPkp(noPkp);
        validasiNpwpSupplierView.setsiteCode(siteCode);
        validasiNpwpSupplierView.executeQuery();

        long npwpSupplierNumRow =
            validasiNpwpSupplierView.getEstimatedRowCount();

        TtfFpUpdateValidateViewImpl ttfFpUpdateValidateView =
            getTtfFpUpdateValidateView1();
        ttfFpUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateView.executeQuery();

        if (npwpSupplierNumRow == 0) {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpSupVal("F");
        } else {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpSupVal(null);
        }
    }

    private void validasiPkpIndomarco(String keyFpBpb, String branchCode,
                                      String pkpIndomarco) {
        ValidasiNpwpIndomarcoViewImpl validasiNpwpIndomarcoView;
        validasiNpwpIndomarcoView = getValidasiNpwpIndomarcoView1();
        validasiNpwpIndomarcoView.setbranchCode(branchCode);
        validasiNpwpIndomarcoView.setnoPkp(pkpIndomarco);
        validasiNpwpIndomarcoView.executeQuery();

        long npwpIndomarcoNumRow =
            validasiNpwpIndomarcoView.getEstimatedRowCount();

        TtfFpUpdateValidateViewImpl ttfFpUpdateValidateView =
            getTtfFpUpdateValidateView1();
        ttfFpUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateView.executeQuery();

        if (npwpIndomarcoNumRow == 0) {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpIdmVal("F");
        } else {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpIdmVal(null);
        }
    }

    private void validasiFormatFp(String keyFpBpb, String noFp) {
        Pattern pattern = Pattern.compile("\\d{3}.\\d{3}-\\d{2}.\\d{8}");
        Matcher matcher = pattern.matcher(noFp);

        TtfFpUpdateValidateViewImpl ttfFpUpdateValidateView =
            getTtfFpUpdateValidateView1();
        ttfFpUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateView.executeQuery();

        if (!matcher.matches()) {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setNoFpVal("F");
        } else {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setNoFpVal(null);
        }
    }

    private void validasiNoBpb(String keyFpBpb, String noBpb, String supCode) {
        ValidasiNoBpbViewImpl validasiNoBpbView;
        validasiNoBpbView = getValidasiNoBpbView1();
        validasiNoBpbView.setnoBpb(noBpb);
        validasiNoBpbView.setsupCode(supCode);
        validasiNoBpbView.executeQuery();

        long validasiNoBpbNumRow = validasiNoBpbView.getEstimatedRowCount();

        TtfBpbUpdateValidateViewImpl ttfBpbUpdateValidateView =
            getTtfBpbUpdateValidateView1();
        ttfBpbUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfBpbUpdateValidateView.setnoBpb(noBpb);
        ttfBpbUpdateValidateView.executeQuery();

        if (validasiNoBpbNumRow == 0) {
            TtfBpbUpdateValidateViewRowImpl ttfBpbUpdateValidateRow =
                (TtfBpbUpdateValidateViewRowImpl)ttfBpbUpdateValidateView.first();
            ttfBpbUpdateValidateRow.setInvoiceNumVal("F");
        } else {
            TtfBpbUpdateValidateViewRowImpl ttfBpbUpdateValidateRow =
                (TtfBpbUpdateValidateViewRowImpl)ttfBpbUpdateValidateView.first();
            ttfBpbUpdateValidateRow.setInvoiceNumVal(null);
        }
    }

    private void validasiTglBpb(String keyTtf, String noBpb) {

        // SYARAT VALIDASI LVL2:
        // 1. Syarat No BPB sudah pasti ada di list tabel master BPB
        Date tglNow = new Date();
        Date tglBpbMax;

        ValidasiTglBpbViewImpl validasiTglBpbView;
        validasiTglBpbView = getValidasiTglBpbView1();
        validasiTglBpbView.setkeyTtf(keyTtf);
        validasiTglBpbView.setnoBpb(noBpb);
        validasiTglBpbView.executeQuery();

        long validasiTglBpbNumRow = validasiTglBpbView.getEstimatedRowCount();

        if (validasiTglBpbNumRow > 0) {
            ValidasiTglBpbViewRowImpl validasiTglBpbRow =
                (ValidasiTglBpbViewRowImpl)validasiTglBpbView.first();
            String tglBpbMaxStr = validasiTglBpbRow.getTglBpbMax().toString();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


            try {
                tglBpbMax = sdf.parse(tglBpbMaxStr);
            } catch (ParseException e) {
                System.out.println("ERROR: Date Conversion.");
                tglBpbMax = new Date();
            }

            TtfBpbUpdateValidateLvl2ViewImpl ttfBpbUpdateValidateLvl2View =
                getTtfBpbUpdateValidateLvl2View1();
            ttfBpbUpdateValidateLvl2View.setkeyTtf(keyTtf);
            ttfBpbUpdateValidateLvl2View.setnoBpb(noBpb);
            ttfBpbUpdateValidateLvl2View.executeQuery();

            if (tglNow.compareTo(tglBpbMax) > 0) {
                TtfBpbUpdateValidateLvl2ViewRowImpl ttfBpbUpdateValidateLvl2Row =
                    (TtfBpbUpdateValidateLvl2ViewRowImpl)ttfBpbUpdateValidateLvl2View.first();
                ttfBpbUpdateValidateLvl2Row.setTglBpbVal("F");
            } else {
                TtfBpbUpdateValidateLvl2ViewRowImpl ttfBpbUpdateValidateLvl2Row =
                    (TtfBpbUpdateValidateLvl2ViewRowImpl)ttfBpbUpdateValidateLvl2View.first();
                ttfBpbUpdateValidateLvl2Row.setTglBpbVal(null);
            }

        }

    }

    private void validasiTglFp(String keyFpBpb, Number amtTax) {

        ValidasiTglFpViewImpl validasiTglFpView;
        validasiTglFpView = getValidasiTglFpView1();
        validasiTglFpView.setkeyFpBpb(keyFpBpb);
        validasiTglFpView.executeQuery();

        long validasiTglFpNumRow = validasiTglFpView.getEstimatedRowCount();

        TtfFpUpdateValidateLvl2ViewImpl ttfFpUpdateValidateLvl2View =
            getTtfFpUpdateValidateLvl2View1();
        ttfFpUpdateValidateLvl2View.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateLvl2View.executeQuery();

        if (validasiTglFpNumRow == 0) {
            TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
            ttfFpUpdateValidateLvl2Row.setTglFpVal(null);
            ttfFpUpdateValidateLvl2Row.setStatus("YCR");
            ttfFpUpdateValidateLvl2Row.setAmountPenggantian(null);
        } else {
            TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
            ttfFpUpdateValidateLvl2Row.setTglFpVal("F");
            ttfFpUpdateValidateLvl2Row.setStatus("KDL");
            ttfFpUpdateValidateLvl2Row.setAmountPenggantian(amtTax);
        }
    }

    private void validasiNilaiDpp(String keyFpBpb) {

        ValidasiNilaiDppViewImpl validasiNilaiDppView;
        validasiNilaiDppView = getValidasiNilaiDppView1();
        validasiNilaiDppView.setkeyFpBpb(keyFpBpb);
        validasiNilaiDppView.executeQuery();

        long validasiNilaiDppNumRow =
            validasiNilaiDppView.getEstimatedRowCount();

        if (validasiNilaiDppNumRow > 0) {
            ValidasiNilaiDppViewRowImpl validasiNilaiDppRow =
                (ValidasiNilaiDppViewRowImpl)validasiNilaiDppView.first();

            BigDecimal amtDppFp =
                new BigDecimal(validasiNilaiDppRow.getAmountDpp().toString());
            BigDecimal totalDppBpb =
                new BigDecimal(validasiNilaiDppRow.getTotalDpp().toString());
            BigDecimal dppTolerance = nilaiToleransiDpp();

            BigDecimal selisihBpbFp = totalDppBpb.subtract(amtDppFp).abs();
            /*
            System.out.println("----------------------------------");
            System.out.println("AMT FP   : " + validasiNilaiDppRow.getAmountDpp().toString());
            System.out.println("TOT DPP  : " + validasiNilaiDppRow.getTotalDpp().toString());
            System.out.println("TOLERANSI: " + nilaiToleransiDpp());
            System.out.println("MIN AMT  : " + minAmount.toString());
            System.out.println("----------------------------------");
            */
            TtfFpUpdateValidateLvl2ViewImpl ttfFpUpdateValidateLvl2View =
                getTtfFpUpdateValidateLvl2View1();
            ttfFpUpdateValidateLvl2View.setkeyFpBpb(keyFpBpb);
            ttfFpUpdateValidateLvl2View.executeQuery();

            if (selisihBpbFp.compareTo(dppTolerance) > 0) {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtDppVal("F");
            } else {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtDppVal(null);
            }

        }

    }

    private void validasiNilaiPpn(String keyFpBpb) {

        ValidasiNilaiTaxViewImpl validasiNilaiTaxView;
        validasiNilaiTaxView = getValidasiNilaiTaxView1();
        validasiNilaiTaxView.setkeyFpBpb(keyFpBpb);
        validasiNilaiTaxView.executeQuery();

        long validasiNilaiTaxNumRow =
            validasiNilaiTaxView.getEstimatedRowCount();

        if (validasiNilaiTaxNumRow > 0) {
            ValidasiNilaiTaxViewRowImpl validasiNilaiTaxRow =
                (ValidasiNilaiTaxViewRowImpl)validasiNilaiTaxView.first();

            BigDecimal amtTaxFp =
                new BigDecimal(validasiNilaiTaxRow.getAmountTax().toString());
            BigDecimal totalTaxBpb =
                new BigDecimal(validasiNilaiTaxRow.getTotalTax().toString());
            BigDecimal taxTolerance = nilaiToleransiPpn();

            BigDecimal selisihPpnBpbFp = totalTaxBpb.subtract(amtTaxFp).abs();

            TtfFpUpdateValidateLvl2ViewImpl ttfFpUpdateValidateLvl2View =
                getTtfFpUpdateValidateLvl2View1();
            ttfFpUpdateValidateLvl2View.setkeyFpBpb(keyFpBpb);
            ttfFpUpdateValidateLvl2View.executeQuery();

            if (selisihPpnBpbFp.compareTo(taxTolerance) > 0) {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtTaxVal("F");
            } else {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtTaxVal(null);
            }

        }

    }

    private BigDecimal nilaiToleransiDpp() {
        BigDecimal toleransiDpp = BigDecimal.ZERO;

        NilaiToleransiDppViewImpl nilaiToleransiDppView =
            getNilaiToleransiDppView1();
        nilaiToleransiDppView.executeQuery();

        long nilaiToleransiDppNumRow =
            nilaiToleransiDppView.getEstimatedRowCount();

        if (nilaiToleransiDppNumRow > 0) {
            NilaiToleransiDppViewRowImpl nilaiToleransiDppViewRow =
                (NilaiToleransiDppViewRowImpl)nilaiToleransiDppView.first();

            toleransiDpp =
                    new BigDecimal(nilaiToleransiDppViewRow.getParamValue());
        } else {
            System.out.println("------------------------------------------------------");
            System.out.println("ERROR: Konfigurasi parameter toleransi tidak ditemukan");
            System.out.println("------------------------------------------------------");
        }

        return toleransiDpp;
    }

    private BigDecimal nilaiToleransiPpn() {
        BigDecimal toleransiPpn = BigDecimal.ZERO;

        NilaiToleransiPpnViewImpl nilaiToleransiPpnView =
            getNilaiToleransiPpnView1();
        nilaiToleransiPpnView.executeQuery();

        long nilaiToleransiPpnNumRow =
            nilaiToleransiPpnView.getEstimatedRowCount();

        if (nilaiToleransiPpnNumRow > 0) {
            NilaiToleransiPpnViewRowImpl nilaiToleransiPpnViewRow =
                (NilaiToleransiPpnViewRowImpl)nilaiToleransiPpnView.first();

            toleransiPpn =
                    new BigDecimal(nilaiToleransiPpnViewRow.getParamValue());
        } else {
            System.out.println("------------------------------------------------------");
            System.out.println("ERROR: Konfigurasi parameter toleransi tidak ditemukan");
            System.out.println("------------------------------------------------------");
        }

        return toleransiPpn;
    }

    /**
     * Container's getter for TtfFpValidationView1.
     * @return TtfFpValidationView1
     */
    public TtfFpValidationViewImpl getTtfFpValidationView1() {
        return (TtfFpValidationViewImpl)findViewObject("TtfFpValidationView1");
    }

    /**
     * Container's getter for TtfBpbValidationView1.
     * @return TtfBpbValidationView1
     */
    public TtfBpbValidationViewImpl getTtfBpbValidationView1() {
        return (TtfBpbValidationViewImpl)findViewObject("TtfBpbValidationView1");
    }

    /**
     * Container's getter for ValidasiNpwpSupplierView1.
     * @return ValidasiNpwpSupplierView1
     */
    public ValidasiNpwpSupplierViewImpl getValidasiNpwpSupplierView1() {
        return (ValidasiNpwpSupplierViewImpl)findViewObject("ValidasiNpwpSupplierView1");
    }

    /**
     * Container's getter for TtfFpUpdateValidateView1.
     * @return TtfFpUpdateValidateView1
     */
    public TtfFpUpdateValidateViewImpl getTtfFpUpdateValidateView1() {
        return (TtfFpUpdateValidateViewImpl)findViewObject("TtfFpUpdateValidateView1");
    }

    /**
     * Container's getter for ValidasiNpwpIndomarcoView1.
     * @return ValidasiNpwpIndomarcoView1
     */
    public ValidasiNpwpIndomarcoViewImpl getValidasiNpwpIndomarcoView1() {
        return (ValidasiNpwpIndomarcoViewImpl)findViewObject("ValidasiNpwpIndomarcoView1");
    }

    /**
     * Container's getter for TtfBpbUpdateValidateView1.
     * @return TtfBpbUpdateValidateView1
     */
    public TtfBpbUpdateValidateViewImpl getTtfBpbUpdateValidateView1() {
        return (TtfBpbUpdateValidateViewImpl)findViewObject("TtfBpbUpdateValidateView1");
    }

    /**
     * Container's getter for ValidasiNoBpbView1.
     * @return ValidasiNoBpbView1
     */
    public ValidasiNoBpbViewImpl getValidasiNoBpbView1() {
        return (ValidasiNoBpbViewImpl)findViewObject("ValidasiNoBpbView1");
    }

    /**
     * Container's getter for ValidasiTglBpbView1.
     * @return ValidasiTglBpbView1
     */
    public ValidasiTglBpbViewImpl getValidasiTglBpbView1() {
        return (ValidasiTglBpbViewImpl)findViewObject("ValidasiTglBpbView1");
    }

    /**
     * Container's getter for TtfBpbUpdateValidateLvl2View1.
     * @return TtfBpbUpdateValidateLvl2View1
     */
    public TtfBpbUpdateValidateLvl2ViewImpl getTtfBpbUpdateValidateLvl2View1() {
        return (TtfBpbUpdateValidateLvl2ViewImpl)findViewObject("TtfBpbUpdateValidateLvl2View1");
    }

    /**
     * Container's getter for ValidasiTglFpView1.
     * @return ValidasiTglFpView1
     */
    public ValidasiTglFpViewImpl getValidasiTglFpView1() {
        return (ValidasiTglFpViewImpl)findViewObject("ValidasiTglFpView1");
    }

    /**
     * Container's getter for TtfFpUpdateValidateLvl2View1.
     * @return TtfFpUpdateValidateLvl2View1
     */
    public TtfFpUpdateValidateLvl2ViewImpl getTtfFpUpdateValidateLvl2View1() {
        return (TtfFpUpdateValidateLvl2ViewImpl)findViewObject("TtfFpUpdateValidateLvl2View1");
    }

    /**
     * Container's getter for ValidasiNilaiDppView1.
     * @return ValidasiNilaiDppView1
     */
    public ValidasiNilaiDppViewImpl getValidasiNilaiDppView1() {
        return (ValidasiNilaiDppViewImpl)findViewObject("ValidasiNilaiDppView1");
    }

    /**
     * Container's getter for ValidasiNilaiTaxView1.
     * @return ValidasiNilaiTaxView1
     */
    public ValidasiNilaiTaxViewImpl getValidasiNilaiTaxView1() {
        return (ValidasiNilaiTaxViewImpl)findViewObject("ValidasiNilaiTaxView1");
    }

    /**
     * Container's getter for NilaiToleransiDppView1.
     * @return NilaiToleransiDppView1
     */
    public NilaiToleransiDppViewImpl getNilaiToleransiDppView1() {
        return (NilaiToleransiDppViewImpl)findViewObject("NilaiToleransiDppView1");
    }

    /**
     * Container's getter for NilaiToleransiPpnView1.
     * @return NilaiToleransiPpnView1
     */
    public NilaiToleransiPpnViewImpl getNilaiToleransiPpnView1() {
        return (NilaiToleransiPpnViewImpl)findViewObject("NilaiToleransiPpnView1");
    }

    public void insertTtfNum(String keyTtf, String kodeBranch) {

        Date dateNow = new Date();
        yearNowStr = new StringBuilder(dateYearFormat.format(dateNow));

        Connection conn = null;
        String ttfNumber = "";
        Integer rsNumRow = 0;
        
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("jdbc/imttfOnlineDS");
            conn = ds.getConnection();
            
            PreparedStatement statement =
                conn.prepareStatement("SELECT TNSEQ.LAST_SEQ FROM IMTTF.TTF_NUM_SEQ TNSEQ WHERE TNSEQ.TAHUN=TO_CHAR(SYSDATE, 'YY') FOR UPDATE");

            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                Integer nextSeq =
                    Integer.valueOf(rs.getString("LAST_SEQ")) + 1;
                String nextSeqStr = String.format("%05d", nextSeq);
                ttfNumber = yearNowStr + kodeBranch + ONLINE_FLAG + nextSeqStr;

                try {
                    PreparedStatement updateTtfNumSeq =
                        conn.prepareStatement("UPDATE IMTTF.TTF_NUM_SEQ TNSEQ SET TNSEQ.LAST_SEQ=? WHERE TNSEQ.TAHUN=TO_CHAR(SYSDATE, 'YY')");
                    updateTtfNumSeq.setInt(1, nextSeq);
                    updateTtfNumSeq.executeUpdate();
                    updateTtfNumSeq.close();
                } catch (SQLException sqle) {
                    System.out.println("------------------------------------------------");
                    System.out.println("ERROR: Cannot run update query");
                    System.out.println("STACK: " + sqle.toString().trim());
                    System.out.println("------------------------------------------------");
                }
                rsNumRow = rsNumRow + 1;
            }
            
            if (rsNumRow == 0) {
                ttfNumber = yearNowStr + kodeBranch + ONLINE_FLAG + "00001";
                try {
                    PreparedStatement updateTtfNumSeq =
                        conn.prepareStatement("INSERT INTO IMTTF.TTF_NUM_SEQ TNSEQ (TNSEQ.TAHUN, TNSEQ.LAST_SEQ) VALUES (?, 1)");
                    updateTtfNumSeq.setString(1, yearNowStr.toString());
                    updateTtfNumSeq.executeUpdate();
                    updateTtfNumSeq.close();
                } catch (SQLException sqle) {
                    System.out.println("------------------------------------------------");
                    System.out.println("ERROR: Cannot run update query");
                    System.out.println("STACK: " + sqle.toString().trim());
                    System.out.println("------------------------------------------------");
                }
            }

            statement.close();
            rs.close();
            conn.close();
        } catch (Exception exc) {
            exc.printStackTrace();
        }

        // Generate Barcode
        BlobDomain ttfBarcode = genBarcodeInsert(ttfNumber);

        // Update TTF Header Amount Total
        NumberTtfHeaderViewImpl numberTtfHeader =
            this.getNumberTtfHeaderView1();
        numberTtfHeader.setkeyTtf(keyTtf);
        numberTtfHeader.executeQuery();

        try {
            NumberTtfHeaderViewRowImpl numberTtfHeaderRow =
                (NumberTtfHeaderViewRowImpl)numberTtfHeader.getRowSetIterator().next();
            numberTtfHeaderRow.setTtfNum(ttfNumber);
            numberTtfHeaderRow.setTtfBarcode(ttfBarcode);
        } finally {
            getDBTransaction().commit();
        }

    }

    // Generate Barcode

    private BlobDomain genBarcodeInsert(String ttfNumber) {
        BlobDomain barcodeTtfNum;

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //EAN13Bean barCode = new EAN13Bean();
        Code128Bean barCode = new Code128Bean();
        boolean antiAlias = false;
        int orientation = 0;
        final int dpi = 160;

        //Configure the barcode generator
        barCode.setModuleWidth(UnitConv.in2mm(2.8f / dpi));

        BitmapCanvasProvider canvas =
            new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY,
                                     antiAlias, orientation);

        BarcodeDimension dim = new BarcodeDimension(120, 1);
        canvas.establishDimensions(dim);
        barCode.generateBarcode(canvas, ttfNumber);

        try {
            canvas.finish();

            String mime = MimeTypes.MIME_PNG;
            os = new ByteArrayOutputStream();

            final BitmapEncoder encoder =
                BitmapEncoderRegistry.getInstance(mime);

            encoder.encode(canvas.getBufferedImage(), os, mime, dpi);
        } catch (IOException e) {
        }

        barcodeTtfNum = new BlobDomain(os.toByteArray());

        return barcodeTtfNum;
    }

    /**
     * Container's getter for NumberTtfHeaderView1.
     * @return NumberTtfHeaderView1
     */
    public NumberTtfHeaderViewImpl getNumberTtfHeaderView1() {
        return (NumberTtfHeaderViewImpl)findViewObject("NumberTtfHeaderView1");
    }

    public void amountTotalTtfNonPkp(String keyTtf) {

        BigDecimal ttfAmountTotal = BigDecimal.ZERO;

        TtfBpbTotalAmountViewImpl ttfBpbTotalAmount =
            this.getTtfBpbTotalAmountView1();
        ttfBpbTotalAmount.setkeyTtf(keyTtf);
        ttfBpbTotalAmount.executeQuery();

        long ttfBpbTotalAmountRowCount =
            ttfBpbTotalAmount.getEstimatedRowCount();

        if (ttfBpbTotalAmountRowCount > 0) {
            for (int i = 0; i < ttfBpbTotalAmountRowCount; i++) {
                TtfBpbTotalAmountViewRowImpl ttfBpbTotalAmountRow;
                ttfBpbTotalAmountRow =
                        (TtfBpbTotalAmountViewRowImpl)ttfBpbTotalAmount.getRowSetIterator().next();

                ttfAmountTotal =
                        ttfAmountTotal.add(new BigDecimal(ttfBpbTotalAmountRow.getAmountTotal().toString()));

            }
        }

        // Update TTF Header Amount Total
        TtfHeaderTotalAmountViewImpl ttfHeader =
            this.getTtfHeaderTotalAmountView1();
        ttfHeader.setkeyTtf(keyTtf);
        ttfHeader.executeQuery();

        long ttfHeaderNumRow = ttfHeader.getEstimatedRowCount();

        if (ttfHeaderNumRow > 0) {
            try {
                TtfHeaderTotalAmountViewRowImpl ttfHeaderTotalAmountRow =
                    (TtfHeaderTotalAmountViewRowImpl)ttfHeader.first();
                ttfHeaderTotalAmountRow.setTtfAmount(ttfAmountTotal);
            } finally {
                getDBTransaction().commit();
            }
        } else {
            System.out.println("-----------------------------------------------");
            System.out.println("TTF Header with key : " + keyTtf +
                               " not found.");
            System.out.println("-----------------------------------------------");
        }
    }

    public void amountTotalTtfPkp(String keyTtf) {

        BigDecimal ttfAmountTotal = BigDecimal.ZERO;

        TtfFpTotalAmountViewImpl ttfFpTotalAmount =
            this.getTtfFpTotalAmountView1();
        ttfFpTotalAmount.setkeyTtf(keyTtf);
        ttfFpTotalAmount.executeQuery();

        long ttfFpTotalAmountRowCount =
            ttfFpTotalAmount.getEstimatedRowCount();

        if (ttfFpTotalAmountRowCount > 0) {
            for (int i = 0; i < ttfFpTotalAmountRowCount; i++) {
                TtfFpTotalAmountViewRowImpl ttfFpTotalAmountRow;
                ttfFpTotalAmountRow =
                        (TtfFpTotalAmountViewRowImpl)ttfFpTotalAmount.getRowSetIterator().next();

                ttfAmountTotal =
                        ttfAmountTotal.add(new BigDecimal(ttfFpTotalAmountRow.getAmountTotal()));

            }
        }

        // Update TTF Header Amount Total
        TtfHeaderTotalAmountViewImpl ttfHeader =
            this.getTtfHeaderTotalAmountView1();
        ttfHeader.setkeyTtf(keyTtf);
        ttfHeader.executeQuery();

        long ttfHeaderNumRow = ttfHeader.getEstimatedRowCount();

        if (ttfHeaderNumRow > 0) {
            try {
                TtfHeaderTotalAmountViewRowImpl ttfHeaderTotalAmountRow =
                    (TtfHeaderTotalAmountViewRowImpl)ttfHeader.first();
                ttfHeaderTotalAmountRow.setTtfAmount(ttfAmountTotal);
            } finally {
                getDBTransaction().commit();
            }
        } else {
            System.out.println("-----------------------------------------------");
            System.out.println("TTF Header with key : " + keyTtf +
                               " not found.");
            System.out.println("-----------------------------------------------");
        }

    }

    /**
     * Container's getter for TtfFpTotalAmountView1.
     * @return TtfFpTotalAmountView1
     */
    public TtfFpTotalAmountViewImpl getTtfFpTotalAmountView1() {
        return (TtfFpTotalAmountViewImpl)findViewObject("TtfFpTotalAmountView1");
    }

    /**
     * Container's getter for CheckValLevel1CompleteView1.
     * @return CheckValLevel1CompleteView1
     */
    public CheckValLevel1CompleteViewImpl getCheckValLevel1CompleteView1() {
        return (CheckValLevel1CompleteViewImpl)findViewObject("CheckValLevel1CompleteView1");
    }

    /**
     * Container's getter for CheckValLevel2CompleteView1.
     * @return CheckValLevel2CompleteView1
     */
    public CheckValLevel2CompleteViewImpl getCheckValLevel2CompleteView1() {
        return (CheckValLevel2CompleteViewImpl)findViewObject("CheckValLevel2CompleteView1");
    }

    /**
     * Container's getter for TtfHeaderMemoMdView1.
     * @return TtfHeaderMemoMdView1
     */
    public TtfHeaderMemoMdViewImpl getTtfHeaderMemoMdView1() {
        return (TtfHeaderMemoMdViewImpl)findViewObject("TtfHeaderMemoMdView1");
    }


    /**
     * Container's getter for CheckNilaiDppTidakSamaView1.
     * @return CheckNilaiDppTidakSamaView1
     */
    public ViewObjectImpl getCheckNilaiDppTidakSamaView1() {
        return (ViewObjectImpl)findViewObject("CheckNilaiDppTidakSamaView1");
    }

    /**
     * Container's getter for CheckNilaiTaxTidakSamaView1.
     * @return CheckNilaiTaxTidakSamaView1
     */
    public ViewObjectImpl getCheckNilaiTaxTidakSamaView1() {
        return (ViewObjectImpl)findViewObject("CheckNilaiTaxTidakSamaView1");
    }

    /**
     * Container's getter for CheckTglBpbExpiredView1.
     * @return CheckTglBpbExpiredView1
     */
    public ViewObjectImpl getCheckTglBpbExpiredView1() {
        return (ViewObjectImpl)findViewObject("CheckTglBpbExpiredView1");
    }

    /**
     * Container's getter for CheckTglFpExpiredView1.
     * @return CheckTglFpExpiredView1
     */
    public ViewObjectImpl getCheckTglFpExpiredView1() {
        return (ViewObjectImpl)findViewObject("CheckTglFpExpiredView1");
    }

    /**
     * Container's getter for TtfHeaderTotalAmountView1.
     * @return TtfHeaderTotalAmountView1
     */
    public TtfHeaderTotalAmountViewImpl getTtfHeaderTotalAmountView1() {
        return (TtfHeaderTotalAmountViewImpl)findViewObject("TtfHeaderTotalAmountView1");
    }

    /**
     * Container's getter for TtfHeaderUpdateStatusView1.
     * @return TtfHeaderUpdateStatusView1
     */
    public TtfHeaderUpdateStatusViewImpl getTtfHeaderUpdateStatusView1() {
        return (TtfHeaderUpdateStatusViewImpl)findViewObject("TtfHeaderUpdateStatusView1");
    }

    /**
     * Container's getter for TtfHeaderGetKeyView1.
     * @return TtfHeaderGetKeyView1
     */
    public TtfHeaderGetKeyViewImpl getTtfHeaderGetKeyView1() {
        return (TtfHeaderGetKeyViewImpl)findViewObject("TtfHeaderGetKeyView1");
    }

    /**
     * Container's getter for TtfHeaderReviewUploadView1.
     * @return TtfHeaderReviewUploadView1
     */
    public ViewObjectImpl getTtfHeaderReviewUploadView1() {
        return (ViewObjectImpl)findViewObject("TtfHeaderReviewUploadView1");
    }

    /**
     * Container's getter for TtfBpbUpdateDataView1.
     * @return TtfBpbUpdateDataView1
     */
    public ViewObjectImpl getTtfBpbUpdateDataView1() {
        return (ViewObjectImpl)findViewObject("TtfBpbUpdateDataView1");
    }

    /**
     * Container's getter for TtfFpUpdateDataView1.
     * @return TtfFpUpdateDataView1
     */
    public ViewObjectImpl getTtfFpUpdateDataView1() {
        return (ViewObjectImpl)findViewObject("TtfFpUpdateDataView1");
    }

    /**
     * Container's getter for TtfHeaderReviewUploadNonPkpView1.
     * @return TtfHeaderReviewUploadNonPkpView1
     */
    public ViewObjectImpl getTtfHeaderReviewUploadNonPkpView1() {
        return (ViewObjectImpl)findViewObject("TtfHeaderReviewUploadNonPkpView1");
    }

    /**
     * Container's getter for UserCabangView1.
     * @return UserCabangView1
     */
    public ViewObjectImpl getUserCabangView1() {
        return (ViewObjectImpl)findViewObject("UserCabangView1");
    }


    /**
     * Container's getter for LovSupplier1.
     * @return LovSupplier1
     */
    public LovSupplierImpl getLovSupplier1() {
        return (LovSupplierImpl)findViewObject("LovSupplier1");
    }

    /**
     * Container's getter for UserCabangLovSupplierViewLink1.
     * @return UserCabangLovSupplierViewLink1
     */
    public ViewLinkImpl getUserCabangLovSupplierViewLink1() {
        return (ViewLinkImpl)findViewLink("UserCabangLovSupplierViewLink1");
    }

    /**
     * Container's getter for ValidateSupCodeBranchView1.
     * @return ValidateSupCodeBranchView1
     */
    public ValidateSupCodeBranchViewImpl getValidateSupCodeBranchView1() {
        return (ValidateSupCodeBranchViewImpl)findViewObject("ValidateSupCodeBranchView1");
    }

    /**
     * Container's getter for TtfBpbTotalAmountView1.
     * @return TtfBpbTotalAmountView1
     */
    public TtfBpbTotalAmountViewImpl getTtfBpbTotalAmountView1() {
        return (TtfBpbTotalAmountViewImpl)findViewObject("TtfBpbTotalAmountView1");
    }

    /**
     * Container's getter for ParamGlobalView1.
     * @return ParamGlobalView1
     */
    public ViewObjectImpl getParamGlobalView1() {
        return (ViewObjectImpl)findViewObject("ParamGlobalView1");
    }

    public void genTanggal(String keyTtf, String supCode, String branchCd) {

        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        String flagTransfer = null;
        // Cari Tanggal pada hari Kamis atau Jumat berikutnya dari tanggal tersebut
        ValidTglJatuhTempoViewImpl validTglJatuhTempo =
            this.getValidTglJatuhTempoView1();
        validTglJatuhTempo.setkeyTtf(keyTtf);
        validTglJatuhTempo.executeQuery();

        ValidTglJatuhTempoViewRowImpl validTglJatuhTempoRow =
            (ValidTglJatuhTempoViewRowImpl)validTglJatuhTempo.first();
        oracle.jbo.domain.Date thurFriDate =
            validTglJatuhTempoRow.getValidDate();

        Date tglKetentuanIdm = checkTglKetentuanIdm(thurFriDate);

        SupplierGiroTransferFlagViewImpl supplierGiroTransferFlag =
            (SupplierGiroTransferFlagViewImpl)this.getSupplierGiroTransferFlagView1();
        supplierGiroTransferFlag.setsupCode(supCode);
        supplierGiroTransferFlag.setbranchCode(branchCd);
        supplierGiroTransferFlag.executeQuery();

        long supplierGiroTransferFlagNumRow =
            supplierGiroTransferFlag.getEstimatedRowCount();

        if (supplierGiroTransferFlagNumRow > 0) {
            SupplierGiroTransferFlagViewRowImpl supplierGiroTransferFlagRow =
                (SupplierGiroTransferFlagViewRowImpl)supplierGiroTransferFlag.first();
            flagTransfer =
                    supplierGiroTransferFlagRow.getFlagTransfer().trim();
        } else {
            System.out.println("-----------------------------");
            System.out.println("Kode supplier tidak ditemukan");
            System.out.println("-----------------------------");
        }

        // Update TTF Header Tanggal Due Date Tercetak dan Due Date System
        TanggalTtfHeaderViewImpl tanggalTtfHeader =
            this.getTanggalTtfHeaderView1();
        tanggalTtfHeader.setkeyTtf(keyTtf);
        tanggalTtfHeader.executeQuery();

        if (flagTransfer.equalsIgnoreCase("Y")) {

            String cekDate = formatter.format(tglKetentuanIdm).toString();

            ValidTglWednesdayViewImpl validTglWednesday =
                (ValidTglWednesdayViewImpl)this.getValidTglWednesdayView1();
            validTglWednesday.setcurDate(cekDate);
            validTglWednesday.executeQuery();

            ValidTglWednesdayViewRowImpl validTglWednesdayRow =
                (ValidTglWednesdayViewRowImpl)validTglWednesday.first();
            oracle.jbo.domain.Date validWednesday =
                validTglWednesdayRow.getWedDay();

            Date tglKetentuanIdm2 = checkTglKetentuanIdm(validWednesday);

            // Due date tercetak
            java.util.Date dueDateCetakUtilDate = tglKetentuanIdm;
            java.sql.Date dueDateCetakSqlDate =
                new java.sql.Date(dueDateCetakUtilDate.getTime());
            oracle.jbo.domain.Date dueDateCetak =
                new oracle.jbo.domain.Date(new java.sql.Timestamp(dueDateCetakSqlDate.getTime()));

            // Due date sistem
            java.util.Date dueDateSistemUtilDate = tglKetentuanIdm2;
            java.sql.Date dueDateSistemSqlDate =
                new java.sql.Date(dueDateSistemUtilDate.getTime());
            oracle.jbo.domain.Date dueDateSistem =
                new oracle.jbo.domain.Date(new java.sql.Timestamp(dueDateSistemSqlDate.getTime()));

            try {
                TanggalTtfHeaderViewRowImpl tanggalTtfHeaderRow =
                    (TanggalTtfHeaderViewRowImpl)tanggalTtfHeader.getRowSetIterator().next();
                tanggalTtfHeaderRow.setDueDateTercetak(dueDateCetak);
                tanggalTtfHeaderRow.setDueDateSystem(dueDateSistem);
            } finally {
                getDBTransaction().commit();
            }

        } else {
            // Due date
            java.util.Date dueDateUtilDate = tglKetentuanIdm;
            java.sql.Date dueDateSqlDate =
                new java.sql.Date(dueDateUtilDate.getTime());
            oracle.jbo.domain.Date dueDate =
                new oracle.jbo.domain.Date(new java.sql.Timestamp(dueDateSqlDate.getTime()));

            try {
                TanggalTtfHeaderViewRowImpl tanggalTtfHeaderRow =
                    (TanggalTtfHeaderViewRowImpl)tanggalTtfHeader.getRowSetIterator().next();
                tanggalTtfHeaderRow.setDueDateSystem(dueDate);
                tanggalTtfHeaderRow.setDueDateTercetak(dueDate);
            } finally {
                getDBTransaction().commit();
            }
        }
    }

    private Date checkTglKetentuanIdm(oracle.jbo.domain.Date dateToCheck) {
        Integer dateNum, dateNumAdd = 1;
        Date checkDate = dateToCheck.getValue();

        // Check tanggal berada di tanggal 1 dan 6
        dateNum =
                Integer.valueOf(new SimpleDateFormat("dd").format(checkDate).toString());

        if (dateNum < 6) {
            // Cari selisih tanggal
            for (int i = dateNum; i < 6; i++) {
                dateNumAdd = dateNumAdd + 1;
            }

            // Tambah selisih tanggal
            checkDate = addDays(checkDate, dateNumAdd);
        }

        // Cek apakah ada hari libur Indomarco
        java.util.Date checkDateUtilDate = checkDate;
        java.sql.Date checkDateSqlDate =
            new java.sql.Date(checkDateUtilDate.getTime());
        oracle.jbo.domain.Date tglCekLiburIdm =
            new oracle.jbo.domain.Date(new java.sql.Timestamp(checkDateSqlDate.getTime()));

        for (int j = 0; j < 1; j++) {
            // Check libur di tabel Indomarco
            HariLiburIndomarcoViewImpl hariLiburIndomarco =
                (HariLiburIndomarcoViewImpl)getHariLiburIndomarcoView1();
            hariLiburIndomarco.settglLibur(tglCekLiburIdm);
            hariLiburIndomarco.executeQuery();

            long hariLiburIndomarcoRowNum =
                hariLiburIndomarco.getEstimatedRowCount();
            if (hariLiburIndomarcoRowNum > 0) {
                checkDate = addDays(checkDate, 1);

                // Check apakah hari SABTU / MINGGU
                if (DateToCalendar(checkDate).get(Calendar.DAY_OF_WEEK) ==
                    Calendar.SATURDAY) {
                    checkDate = addDays(checkDate, 1);
                }
                if (DateToCalendar(checkDate).get(Calendar.DAY_OF_WEEK) ==
                    Calendar.SUNDAY) {
                    checkDate = addDays(checkDate, 1);
                }
                j = 0;
            } else {
                // Check apakah hari SABTU / MINGGU
                if (DateToCalendar(checkDate).get(Calendar.DAY_OF_WEEK) ==
                    Calendar.SATURDAY) {
                    checkDate = addDays(checkDate, 1);
                }
                if (DateToCalendar(checkDate).get(Calendar.DAY_OF_WEEK) ==
                    Calendar.SUNDAY) {
                    checkDate = addDays(checkDate, 1);
                }
                j = 1;
            }
        }
        return checkDate;
    }


    public static Calendar DateToCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    /**
     * Container's getter for HariLiburIndomarcoView1.
     * @return HariLiburIndomarcoView1
     */
    public ViewObjectImpl getHariLiburIndomarcoView1() {
        return (ViewObjectImpl)findViewObject("HariLiburIndomarcoView1");
    }

    /**
     * Container's getter for SupplierGiroTransferFlagView1.
     * @return SupplierGiroTransferFlagView1
     */
    public SupplierGiroTransferFlagViewImpl getSupplierGiroTransferFlagView1() {
        return (SupplierGiroTransferFlagViewImpl)findViewObject("SupplierGiroTransferFlagView1");
    }

    /**
     * Container's getter for ValidTglWednesdayView1.
     * @return ValidTglWednesdayView1
     */
    public ViewObjectImpl getValidTglWednesdayView1() {
        return (ViewObjectImpl)findViewObject("ValidTglWednesdayView1");
    }

    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); //minus number would decrement the days
        return cal.getTime();
    }

    /**
     * Container's getter for TanggalTtfHeaderView1.
     * @return TanggalTtfHeaderView1
     */
    public TanggalTtfHeaderViewImpl getTanggalTtfHeaderView1() {
        return (TanggalTtfHeaderViewImpl)findViewObject("TanggalTtfHeaderView1");
    }

    /**
     * Container's getter for ValidTglJatuhTempoView1.
     * @return ValidTglJatuhTempoView1
     */
    public ValidTglJatuhTempoViewImpl getValidTglJatuhTempoView1() {
        return (ValidTglJatuhTempoViewImpl)findViewObject("ValidTglJatuhTempoView1");
    }

    /**
     * Container's getter for DeleteTtfBpbWhenCancelView1.
     * @return DeleteTtfBpbWhenCancelView1
     */
    public DeleteTtfBpbWhenCancelViewImpl getDeleteTtfBpbWhenCancelView1() {
        return (DeleteTtfBpbWhenCancelViewImpl)findViewObject("DeleteTtfBpbWhenCancelView1");
    }

    /**
     * Container's getter for DeleteTtfFpWhenCancelView1.
     * @return DeleteTtfFpWhenCancelView1
     */
    public DeleteTtfFpWhenCancelViewImpl getDeleteTtfFpWhenCancelView1() {
        return (DeleteTtfFpWhenCancelViewImpl)findViewObject("DeleteTtfFpWhenCancelView1");
    }

    /**
     * Container's getter for DeleteTtfHeaderWhenCancelView1.
     * @return DeleteTtfHeaderWhenCancelView1
     */
    public DeleteTtfHeaderWhenCancelViewImpl getDeleteTtfHeaderWhenCancelView1() {
        return (DeleteTtfHeaderWhenCancelViewImpl)findViewObject("DeleteTtfHeaderWhenCancelView1");
    }

}
